/**
 * 
 */
package br.com.ctm.advogadope.dao.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;

import br.com.ctm.advogadope.dao.api.ProcessoDao;
import br.com.ctm.advogadope.model.Advogado;
import br.com.ctm.advogadope.model.Processo;
import br.com.framework.persistencia.impl.BaseDaoImpl;
import br.com.framework.persistencia.model.AuditedEntity;
import br.com.framework.persistencia.model.AuditedEntity.Status;

/**
 * @author Cleber
 *
 */
@Stateless
public class ProcessoDaoImpl extends BaseDaoImpl<Processo> implements ProcessoDao{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 */
	public ProcessoDaoImpl() {
		super(Processo.class);
	}

	/* (non-Javadoc)
	 * @see br.com.ctm.advogadope.dao.api.ProcessoDao#findByAdvogado(br.com.ctm.advogadope.model.Advogado, int, int, boolean)
	 */
	@Override
	public List<Processo> findByAdvogado(Advogado advogado, int firstResult,
			int maxResults, boolean onlyActive) throws PersistenceException {
		StringBuffer qryStr = new StringBuffer();
		qryStr.append("SELECT p FROM Advogado adv JOIN adv.processos p WHERE adv = :advogado ");
		if (onlyActive && AuditedEntity.class.isAssignableFrom(getEntityClass())) {
	    	qryStr.append("AND p.status = :status ");
	    }
		TypedQuery<Processo> query = getEntityManager().createQuery(
				qryStr.toString(), Processo.class);
		query.setParameter("advogado", advogado);
		if (onlyActive && AuditedEntity.class.isAssignableFrom(getEntityClass())) {
	    	query.setParameter("status", Status.ACTIVE);
	    }
		if (firstResult > -1 && maxResults > -1){
	    	query.setFirstResult(firstResult);
	    	query.setMaxResults(maxResults);
		}
		return query.getResultList();
	}

	@Override
	public List<Processo> findByFiltroEAdvogado(Processo filtro,
			Advogado advogado, int firstResult, int maxResults, boolean onlyActive)
			throws PersistenceException {
		StringBuffer qryStr = new StringBuffer();
		qryStr.append("SELECT p FROM Advogado adv JOIN c.processos p WHERE adv = :advogado ");
		if (onlyActive && AuditedEntity.class.isAssignableFrom(getEntityClass())) {
	    	qryStr.append("AND p.status = :status ");
	    }
		if (filtro.getNpu() != null && !filtro.getNpu().trim().isEmpty()) {
			qryStr.append("AND p.npu = :npu ");
		}
		if (filtro.getTipoJuizo() != null) {
			qryStr.append("AND p.tipoJuizo = :tipoJuizo ");
		}
		if (filtro.getTribunal() != null) {
			qryStr.append("AND p.tribunal = :tribunal ");
		}
		TypedQuery<Processo> query = getEntityManager().createQuery(
				qryStr.toString(), Processo.class);
		query.setParameter("advogado", advogado);
		if (onlyActive && AuditedEntity.class.isAssignableFrom(getEntityClass())) {
	    	query.setParameter("status", Status.ACTIVE);
	    }
		if (filtro.getNpu() != null && !filtro.getNpu().trim().isEmpty()) {
			query.setParameter("npu", filtro.getNpu());
		}
		if (filtro.getTipoJuizo() != null) {
			query.setParameter("tipoJuizo", filtro.getTipoJuizo());
		}
		if (filtro.getTribunal() != null) {
			query.setParameter("tribunal", filtro.getTribunal());
		}
		if (firstResult > -1 && maxResults > -1){
	    	query.setFirstResult(firstResult);
	    	query.setMaxResults(maxResults);
		}
		return query.getResultList();
	}

	@Override
	public List<Processo> findByDtAtualizacaoMaiorQueIntervalo(int intervalo,
			int firstResult, int maxResults, boolean onlyActive)
			throws PersistenceException {
		StringBuffer qryStr = new StringBuffer();
		Date dateInterval = new Date(System.currentTimeMillis() - intervalo);
		qryStr.append("SELECT p FROM Processo p WHERE p.dataAtualizacao < :intervalo ");
		if (onlyActive && AuditedEntity.class.isAssignableFrom(getEntityClass())) {
	    	qryStr.append("AND p.status = :status ");
	    }
		
		TypedQuery<Processo> query = getEntityManager().createQuery(
				qryStr.toString(), Processo.class);
		query.setParameter("intervalo", dateInterval);
		if (onlyActive && AuditedEntity.class.isAssignableFrom(getEntityClass())) {
	    	query.setParameter("status", Status.ACTIVE);
	    }
		if (firstResult > -1 && maxResults > -1){
	    	query.setFirstResult(firstResult);
	    	query.setMaxResults(maxResults);
		}
		return query.getResultList();
	}
	
}
